<template>
  <div>
    <div v-if="menu.title.length >= 0 && menu.options.flat().length > 0">
      <div v-if="menu.title.length === 0" class="wholeBox">
        <div
          v-if="isExpanded"
          class="expandableModule"
          @click="toggleProductSelect"
          :class="{ expanded: isProductSelectExpanded }"
        >
          产品选择
        </div>
        <div v-if="isProductSelectExpanded" class="content">
          <div class="radio-buttons-container">
            <div
              v-for="(option, i) in menu.options[0]"
              :key="i"
              class="radio-button"
            >
              <input
                name="radio-group"
                :id="`radio${0}_${i}`"
                class="radio-button__input"
                type="radio"
                :value="`${0}_${i}`"
                v-model="selectProduct"
              />
              <label :for="`radio${0}_${i}`" class="radio-button__label">
                <span class="radio-button__custom"></span>
                {{ option.text }}
              </label>
            </div>
          </div>
        </div>
      </div>
      <template v-else>
        <div v-for="(title, index) in menu.title" :key="title" class="wholeBox">
          <div
            class="expandableModule"
            @click="toggle(title)"
            :class="{ expanded: isExpanded(title) }"
          >
            {{ title }}
          </div>
          <div
            v-if="isExpanded(title) && menu.options[index].length > 0"
            class="content"
          >
            <div class="radio-buttons-container">
              <div
                v-for="(option, i) in menu.options[index]"
                :key="i"
                class="radio-button"
              >
                <input
                  name="radio-group"
                  :id="`radio${index}_${i}`"
                  class="radio-button__input"
                  type="radio"
                  :value="`${index}_${i}`"
                  v-model="selectProduct"
                />
                <label :for="`radio${index}_${i}`" class="radio-button__label">
                  <span class="radio-button__custom"></span>
                  {{ option.text }}
                </label>
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>

    <div
      v-else-if="menu.title.length === 0 && menu.options.flat().length === 0"
    ></div>
  </div>
</template>

<script>
import { forEach } from "lodash";
export default {
  props: {
    menu: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      expandedTitles: {},
      isProductSelectExpanded: false,
      selectProduct: null,
    };
  },
  methods: {
    toggleProductSelect() {
      this.isProductSelectExpanded = !this.isProductSelectExpanded;
    },
    toggle(title) {
      if (this.expandedTitles[title] === undefined) {
        this.expandedTitles = { ...this.expandedTitles, [title]: true };
      } else {
        this.expandedTitles[title] = !this.expandedTitles[title];
      }
    },
    isExpanded(title) {
      return this.expandedTitles[title];
    },
  },
  mounted() {
    this.menu.title.forEach((element) => {
      this.expandedTitles[element] = true;
    });
  },
  watch: {
    selectProduct(newVal) {
      this.$router.push({
        path: this.menu.options[newVal.split("_")[0]][newVal.split("_")[1]]
          .value,
      });
    },
    menu() {
      if (this.menu.options[0][0]) {
        this.$router.push({
          path: this.menu.options[0][0].value,
        });
      }
    },
    $route(to, from) {
      this.selectProduct = to.meta.selectProduct
        ? to.meta.selectProduct
        : "0_0";
    },
  },
};
</script>
<style scoped>
.content {
  width: 100%;
  height: 2vw;
  width: fit-content;
}
.wholeBox {
  border-bottom: solid 10px rgba(186, 226, 245, 1);
}
.expandableModule {
  min-height: 2vw;
  height: auto;
}
.content {
  min-height: 2vw;
  height: auto;
}
.radio-buttons-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  justify-items: start;
  gap: 10px;
  flex-wrap: wrap; /* 允许换行 */
  padding: 1vw;
}

.radio-button {
  display: inline-block;
  position: relative;
  cursor: pointer;
}

.radio-button__input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.radio-button__label {
  display: inline-block;
  padding-left: 30px;
  margin-bottom: 10px;
  position: relative;
  font-size: 16px;
  color: rgba(188, 205, 205, 1);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.23, 1, 0.32, 1);
}

.radio-button__custom {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #555;
  transition: all 0.3s cubic-bezier(0.23, 1, 0.32, 1);
}

.radio-button__input:checked + .radio-button__label .radio-button__custom {
  transform: translateY(-50%) scale(0.9);
  border: 5px solid rgba(34, 181, 179, 1);
  color: rgba(34, 181, 179, 1);
}

.radio-button__input:checked + .radio-button__label {
  color: rgba(34, 181, 179, 1);
}

.radio-button__label:hover .radio-button__custom {
  transform: translateY(-50%) scale(1.2);
  border-color: rgba(34, 181, 179, 1);
  box-shadow: 0 0 10px rgb(26, 140, 138);
}
</style>
